﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HtmlAgilityPack;
using NewsMine.DomainObjects;
using NewsDiscoveryEngine;
using NewsMine.Utilities;

namespace WebApp1.AdminSection
{
    public partial class HintPath : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (WebApp1.Models.SecurityCheckManager.IsAdminPortalEabled() == false)
            {
                Response.Write("You should be the admin....");
                return;
            }

            string url = this.Request.Form.Get("url");
            string menuXpath = this.Request.Form.Get("menuXpath");


            if (string.IsNullOrEmpty(url))
            {
                url = this.Request.QueryString.Get("url");
            }

            if (string.IsNullOrEmpty(url))
                return;

            if (string.IsNullOrEmpty(menuXpath))
                menuXpath = string.Empty;

            Response.Write("Given url is " + url);

            //if (!url.ToLower().StartsWith("http://") || !url.ToLower().StartsWith("https://"))
            //{
            //    Response.Write("Please enter the valid http url");
            //    return;
            //}

            HtmlAgilityPack.HtmlWeb web = new HtmlAgilityPack.HtmlWeb();

            HtmlDocument doc = web.Load(url);

            if (doc == null)
            {
                Response.Write("document is not loaded from the source url <br/>");
                if (doc.ParseErrors != null && doc.ParseErrors.Count() > 0)
                {
                    foreach (var err in doc.ParseErrors)
                    {
                        Response.Write(err.Reason + "<br/>");

                        //DisplayErrorInHtml(err);
                    }
                }

                return;
            }

            WebHelper.ConvertLinksFromRelatetiveToAbsolute(url, doc);

            WebSiteInfo webSiteInfo = new WebSiteInfo();

            Uri uri = new Uri(url);

            webSiteInfo.Domain = uri.DnsSafeHost;

            webSiteInfo.Name = webSiteInfo.Domain;

            webSiteInfo.HomePageUrl = "http://" + webSiteInfo.Domain + "/";


            var htmlLinks = WebHelper.GetHtmlLinks(url, doc, menuXpath + "//a[@href]");

            var sortedLinks = (from lnk in htmlLinks
                               orderby lnk.Title.Length, lnk.Link
                               select lnk).ToList<HtmlLink>();

            foreach (HtmlLink lnk in sortedLinks)
            {
                Response.Write("<table>");


                Response.Write("<tr>");

                Response.Write("<td><h1>");
                Response.Write(lnk.OuterHtml);
                Response.Write("</h1></td>");

                Response.Write("<td>");
               
                if (!lnk.OuterHtml.ToLower().Contains("<img") && lnk.Title.Trim().Length>0)
                    Response.Write("<a href='"+ GetAddMenuLink(lnk) +"' target='blank'>Add as menu</a>");

                Response.Write("</td>");

                Response.Write("<td>");
                Response.Write(lnk.Link);
                Response.Write("</td>");

                Response.Write("</tr>");

                Response.Write("</table>");


                Response.Write("<hr/>");
               
                // Action buttons has to be generated from each link in this output
                // Add new website
                // Add as new Menu
                // Add image as logo
                // Add as fevicon

            }

            //Response.Write("<iframe style='width:50%;height:500px;' src='" + webSiteInfo.HomePageUrl + "'> </iframe>");
        }


        private string GetAddMenuLink(HtmlLink lnk)
        {
            string addMenuLink = "http://localhost/webapp1/Admin/AddMenuInfo?title=" + lnk.Title + "&url=" + lnk.Link;

            addMenuLink = GetBaseUrlwithApplicationPath() + "/Admin/AddMenuInfo?title=" + lnk.Title + "&url=" + lnk.Link;

            return addMenuLink;

        }

        private string GetBaseUrlwithApplicationPath()
        {
            string rawUrl = this.Request.RawUrl;

            return rawUrl.ToLower().Substring(0, rawUrl.ToLower().IndexOf("/adminsection/"));

            //            Request.RawUrl(
            //"/webapp1/AdminSection/WebSitesOverview.aspx"
        }

    }
}